package org.song.filter.authentication;

import org.song.exception.result.AjaxResult;
import org.song.util.JacksonUtil;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Service;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 权限不足处理类
 */
@Service( value = "myAccessDeniedHandler")
public class MyAccessDeniedHandler implements AccessDeniedHandler {

    /**
     * 权限不足处理
     * @param request
     * @param response
     * @param e
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        response.setContentType("application/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.getWriter().write(JacksonUtil.getJsonString(AjaxResult.noPermission("权限不足：" + e.getMessage())));
    }

}
